package com.fanben100.biz;

import java.io.IOException;
import java.io.Serializable;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;

import com.fanben100.domain.LoginModel;
import com.fanben100.util.DateUtil;
import com.fanben100.util.PasswordEncoder;
import com.fanben100.util.ProtocalUtil;
import com.fanben100.util.StringUtil;

/**
 * 登录模板
 * 
 * @author CYC
 * 
 */
public abstract class LoginTemplate {

	@Deprecated
	public final boolean login(LoginModel loginModel) {
		// LoginModel dbLm = findLoginModel(loginModel.getLoginId());
		// if (dbLm != null
		// && !StringUtil.isNullOrEmpty(String.valueOf(loginModel
		// .getLoginId()))) {
		// String encrptPassword = this.encryptPwd(loginModel
		// .getLoginPassword());
		// loginModel.setLoginPassword(encrptPassword);
		// loginModel.setRealName(dbLm.getRealName());
		// return this.match(loginModel, dbLm);
		// }
		return false;
	}

	public final ValidLoginModel directLogin(LoginModel loginModel) {
		ValidLoginModel validModel = null;
		LoginModel httpLm = findLoginModel(loginModel);
		if (httpLm != null) {
			if (httpLm instanceof ValidLoginModel) {
				validModel = ((ValidLoginModel) httpLm);
			}
		}
		return validModel;
	}

	protected String encryptPwd(String loginPassword) {
		// return loginPassword;
		return PasswordEncoder.getMD5(loginPassword.getBytes());
	}

	protected boolean match(LoginModel loginModel, LoginModel dbLm) {
		if (loginModel.getLoginId().equals(dbLm.getLoginId())
				&& loginModel.getLoginPassword()
						.equals(dbLm.getLoginPassword())) {
			return true;
		}
		return false;
	}

	protected abstract LoginModel findLoginModel(Serializable loginId);

	protected LoginModel findLoginModel(LoginModel loginModel) {
		String loginResult = null;
		RequestBuilder requestBuilder = new RequestBuilder();
		requestBuilder = requestBuilder
				.directLogin()
//				.addLoginId(loginModel.getLoginId())
				.addLoginPassword(
						this.encryptPwd(loginModel.getLoginPassword()));
		requestBuilder.build("name", String.valueOf(loginModel.getLoginId()));
		requestBuilder.build("identify", loginModel.getIdentify());
		ValidLoginModel validLoginModel = new ValidLoginModel(loginModel);
		try {
			loginResult = ProtocalUtil.httpRequest(
					new URL(requestBuilder.toString()), null);
		} catch (UnknownHostException une) {
			une.printStackTrace();
			loginResult = "请求失败，无法解析主机,请检查网络连接 " + DateUtil.getCurrentDate();
			validLoginModel.setNetFault(true);
		} catch (ConnectException cne) {
			cne.printStackTrace();
			loginResult = "请求失败，连接超时,请检查网络连接 " + DateUtil.getCurrentDate();
			validLoginModel.setNetFault(true);
		} catch (MalformedURLException e) {
			e.printStackTrace();
			validLoginModel.setNetFault(true);
		} catch (IOException e) {
			e.printStackTrace();
			loginResult = "请求超时，请检查网络连通情况";
			validLoginModel.setNetFault(true);
		}
		validLoginModel.setValidMessage(StringUtil.trimAll(loginResult));
		return validLoginModel;
	}

}
